package com.project.common.tools;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
import com.project.common.constant.RedisConstant;
import com.project.common.domain.entity.Area;
import com.project.common.domain.entity.Dic;
import com.project.common.domain.entity.Industry;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: 陈刚
 * @blame: 从缓存数据获取字典，行业 数据
 * @date: 2022/11/19 21:50
 **/
@Component
public class RedisDataUtils {
    @Resource
    private RedisTemplate redisTemplate;

    private List<Dic> dicList = null;

    private List<Industry> industryList = null;


    private List<Area> areaList = null;
    /**
     * 根据id获取字典名称
     * @param dicId
     * @return
     */
    public String getDicName(Long dicId){
        if(dicId == null){
            return  "" ;
        }
        if(dicList==null){
            String listStr = (String) redisTemplate.opsForValue().get(RedisConstant.DIC_LIST);
            if(StringUtils.isNotBlank(listStr)){
                dicList = JSONUtil.toList(listStr,Dic.class);
            }
        }
        if(ArrayUtil.isNotEmpty(dicList)){
            for (int i=0;i<dicList.size();i++){
                if(dicList.get(i).getId().equals(dicId) || dicList.get(i).getId()==dicId){
                    return dicList.get(i).getName();
                }
            }
        }
        return "";
    }

    public String getIndustryName(Long industryId){
        if(industryList==null){
            String listStr = (String) redisTemplate.opsForValue().get(RedisConstant.INDUSTRY_LIST);
            if(StringUtils.isNotBlank(listStr)){
                industryList = JSONUtil.toList(listStr,Industry.class);
            }
        }
        if(ArrayUtil.isNotEmpty(industryList)){
            for (int i=0;i<industryList.size();i++){
                if(industryList.get(i).getId().equals(industryId) || industryList.get(i).getId()==industryId){
                    return industryList.get(i).getName();
                }
            }
        }
        return "";
    }

    public String getAreaName(String adcode){
        if(areaList==null){
            String listStr = (String) redisTemplate.opsForValue().get(RedisConstant.AREA_LIST);
            if(StringUtils.isNotBlank(listStr)){
                areaList = JSONUtil.toList(listStr,Area.class);
            }
        }
        Area currArea = null ;
        if(ArrayUtil.isNotEmpty(areaList)){
            for (int i=0;i<areaList.size();i++){
                if(areaList.get(i).getAdcode().equals(adcode)){
                    currArea = areaList.get(i);
                    break;
                }
            }
        }
        Area pArea = null ;
        if(currArea !=null && currArea.getPid()!=null){
            for (int i=0;i<areaList.size();i++){
                if(areaList.get(i).getId().equals(currArea.getPid())){
                    pArea = areaList.get(i);
                    break;
                }
            }
        }
        Area topArea = null ;
        if(pArea !=null && pArea.getPid()!=null){
            for (int i=0;i<areaList.size();i++){
                if(areaList.get(i).getId().equals(pArea.getPid())){
                    topArea = areaList.get(i);
                    break;
                }
            }
        }
        if(topArea != null){
            return topArea.getTitle()+"-"+pArea.getTitle()+"-"+currArea.getTitle();
        }else if(pArea != null){
            return pArea.getTitle()+"-"+currArea.getTitle();
        }else if(currArea != null ){
            return currArea.getTitle();
        }
        return "";
    }
}
